#include <stdio.h>
#include<string.h>
#include <stdlib.h>
#include <stdbool.h>

FILE *fp;

bool File_check(char *fname)
{
	FILE *fp;
	_Bool flag;
	//if file not exist,exit.
	fp = fopen( fname , "r+");
	if (fp == NULL)
	{
		printf("Cannot open the file , press any key to exit!");
		getchar();
		exit(1);
		flag = false;
	}
	else
	{
		flag = true;
	}
}

int FileR_O(char *fname)
{
	char ch;
	fp = fopen( fname , "r+");
	while ((ch = fgetc(fp)) != EOF)
	{
		putchar(ch);
	}

	putchar('\n');
	if (ferror(fp))
	{
		puts("something wrong!");
	}
	else
	{
		puts("\nsuccess!");
		fclose(fp);
		return 0;
	}
}

int FileW_I(char *fname)
{
	char str[102] = { 0 }, strTemp[100];
	fp = fopen( fname , "at+");
	printf("Input a string:\n");
	scanf("%s", &strTemp);
	//while ((ch = getchar()) != '\n')
	//gets(strTemp);		
	strcat(str, "\n");
	strcat(str, strTemp);
	fputs(str, fp);
	fclose(fp);
	return 0;
}

int main(int argc, char *argv)
{
	char Fname[50];
	char fname[]={0};
	int choice;
	bool flag;
	printf("choice to do : 1.file R_O  2.file W_I\n");
	scanf("%d", &choice);
	printf("write down what file you want : \n");
	scanf("%s", &Fname);
	sprintf(fname,"%s",Fname);
	File_check(fname);
	if (flag = true && choice == 1)
	{
		FileR_O(fname);
	}
	else if (flag = 1 && choice == 2)
	{
		FileW_I(fname);
		printf("The new file:\n\n");
		FileR_O(fname);
	}
	else
	{
		return -1;
	}
}
